package com.my.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.entity.Menu;
import com.my.entity.Token;
import com.my.entity.UserMenu;
import com.my.service.*;
import com.my.util.Rest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class MenuController {

    @Resource
    private MenuService mService;

    @Resource
    private TokenService tService;

    @Resource
    private UserMenuService umService;

    @Resource
    private UserService uService;

    @Resource
    private StudentService sService;

    @GetMapping("menu")
    public Rest menu(@RequestHeader("authentication-token") String to) {
        Token token = tService.getById(to);
        List<Menu> list;
        Rest rest = Rest.ok();
        if(token.getType() == 1) {
            list = mService.list(new QueryWrapper<Menu>().ne("id", 11).ne("parent_id", 11));
            rest.setData("role", "管理员").setData("name", uService.getById(token.getUserId()).getName());
        }else {
            list = mService.getMenu(token.getUserId(), token.getType());
            rest.setData("role", token.getType() == 2 ? "老师" : "学生").
                    setData("name", token.getType() == 2 ? uService.getById(token.getUserId()).getName() : sService.getById(token.getUserId()).getName());
        }
        return rest.setData("menus", TreeUtil.build(list, 0L, (m, t) -> {
            t.setId(m.getId());
            t.setParentId(m.getParentId());
            t.setName(m.getName());
            t.putExtra("url", m.getUrl());
        }));
    }

    @GetMapping("getUserMenu")
    public Rest getUserMenu(Long userId, Integer type) {
        return Rest.ok().setData("tree", TreeUtil.build(mService.getUserMenu(userId, type), 0L, (m, t) -> {
            t.setId(m.getId());
            t.setParentId(m.getParentId());
            t.putExtra("title", m.getName());
            t.putExtra("checked", m.isChecked() && m.getDeep() != 0);
            t.putExtra("expand", m.getDeep() < 2);
        }));
    }

    private void studentSetMenu(Integer type, Long userId) {
        if(type == 3) {
            for (Long i : StudentController.STUDENT_MENU) {
                umService.save(new UserMenu().setUserId(userId).setType(type).setMenuId(i));
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("menu/userIdMenu")
    public Rest userIdMenu(@RequestParam Long userId, Integer type, @RequestParam(required = false) String[] menus) {
        umService.remove(new QueryWrapper<UserMenu>().eq("user_id", userId).eq("type", type));
        if(menus == null){
            studentSetMenu(type, userId);
            return Rest.ok("角色分配成功！");
        }
        for (Long menu : Convert.toLongArray(menus)) {
            umService.save(new UserMenu().setUserId(userId).setType(type).setMenuId(menu));
        }
        studentSetMenu(type, userId);
        return Rest.ok("角色分配成功！");
    }

}
